
function request({url, data=null, dataType='json', method='GET'} = {}) {
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      data,
      dataType,
      method,
      header: {
        'content-type': 'application/json', // 默认值
        'openid': wx.getStorageSync('openid')
      },
      success({statusCode, data }) {
        if(statusCode >= 200 && statusCode < 300) {
          const { code, msg } = data
          if(code === 0) {
            resolve(data)
          }else if(code === 403) {
            resolve(data)
            wx.reLaunch({ url: '/pages/error/index' })
          }else {
            wx.showToast({ title: msg, icon: 'none', mask: true })
            reject(data)
          }
        }else {
          wx.showToast({ title: '服务器外部错误或网络问题', icon: 'none', mask: true })
          reject(data)
        }
      },
      fail(res) {
        wx.showToast({ title: '网络问题，请重试', icon: 'none', mask: true })
        reject(res)
      }
    })
  })
}

module.exports = {
  request
}

